package com.chen.integral.module.test.service.impl;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.ChartMultiSeriesRenderData;
import com.deepoove.poi.data.ChartSingleSeriesRenderData;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.SeriesRenderData;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: manager-spring-mvc-hb
 * @Description: 处理图表
 * @Author: chenjiwei
 * @Date: 2021-01-03 2:13
 * @Version: 1.0.0
 */
public class WordChartHandler {
    public void test2() throws IOException {

        List<RowRenderData> listRowList = new ArrayList<RowRenderData>();
        //将数据存储为了后边生成图样式
        List<String> devname = new ArrayList<String>();
        List<Double> useRate = new ArrayList<Double>();
        List<Integer> useYear = new ArrayList<Integer>();

        for(int i = 0; i < 5; i++){

            //生成一行数据
            listRowList.add(RowRenderData.build("电灯_"+i,String.valueOf(Math.random()*100)+"%",String.valueOf(i+1)));

            //存入list,为了生成图表
            devname.add("电灯_"+i);
            useRate.add(Math.random()*100);
            useYear.add(i+1);
        }
//        org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException
        Map<String, Object> data = new HashMap<>();
        XWPFTemplate template = XWPFTemplate.compile(new File("F:\\apps-other\\db\\新建 DOCX 文档.doc"));

        // 单独的柱状图
        ChartMultiSeriesRenderData chartMultiSeriesRenderData = new ChartMultiSeriesRenderData();
        chartMultiSeriesRenderData.setChartTitle("myChart2");
        chartMultiSeriesRenderData.setCategories(devname.toArray(new String[devname.size()]));
        List<SeriesRenderData> seriesRenderDatas = new ArrayList<SeriesRenderData>();
        seriesRenderDatas.add(new SeriesRenderData("使用率",useRate.toArray(new Double[useRate.size()])));
        seriesRenderDatas.add(new SeriesRenderData("使用年限",useYear.toArray(new Integer[useYear.size()])));
        chartMultiSeriesRenderData.setSeriesDatas(seriesRenderDatas);

        data.put("myChart2", chartMultiSeriesRenderData);


        // 柱状图、折线图共存
        ChartMultiSeriesRenderData barLine = new ChartMultiSeriesRenderData();
        barLine.setChartTitle("myChart1");
        barLine.setCategories(devname.toArray(new String[devname.size()]));

        List<SeriesRenderData> seriesRenderDatas2 = new ArrayList<SeriesRenderData>();
        SeriesRenderData seriesRenderData1 = new SeriesRenderData();
        seriesRenderData1.setName("使用率bar");
        seriesRenderData1.setValues(useRate.toArray(new Double[useRate.size()]));
        seriesRenderData1.setComboType(SeriesRenderData.ComboType.BAR);
        seriesRenderDatas2.add(seriesRenderData1);

        SeriesRenderData seriesRenderData2 = new SeriesRenderData();
        seriesRenderData2.setName("使用年限line");
        seriesRenderData2.setValues(useYear.toArray(new Integer[useYear.size()]));
        seriesRenderData2.setComboType(SeriesRenderData.ComboType.LINE);
        seriesRenderDatas2.add(seriesRenderData2);

        SeriesRenderData seriesRenderData3 = new SeriesRenderData();
        seriesRenderData3.setName("使用率line");
        seriesRenderData3.setValues(useRate.toArray(new Double[useRate.size()]));
        seriesRenderData3.setComboType(SeriesRenderData.ComboType.LINE);
        seriesRenderDatas2.add(seriesRenderData3);

        SeriesRenderData seriesRenderData4 = new SeriesRenderData();
        seriesRenderData4.setName("使用年限bar");
        seriesRenderData4.setValues(useYear.toArray(new Integer[useYear.size()]));
        seriesRenderData4.setComboType(SeriesRenderData.ComboType.BAR);
        seriesRenderDatas2.add(seriesRenderData4);

        barLine.setSeriesDatas(seriesRenderDatas2);
        data.put("myChart1",barLine);


        // 饼状图
        ChartSingleSeriesRenderData pie = new ChartSingleSeriesRenderData();
        pie.setChartTitle("myChart3");
        pie.setCategories(devname.toArray(new String[devname.size()]));
        pie.setSeriesData(new SeriesRenderData("电灯数量",new Integer[]{120,25,89,65,49}));
        data.put("myChart3", pie);


        ChartMultiSeriesRenderData pie2 = new ChartMultiSeriesRenderData();
        pie2.setChartTitle("myChart4");
        pie2.setCategories(devname.toArray(new String[devname.size()]));
        List<SeriesRenderData> seriesRenderDatasPie = new ArrayList<SeriesRenderData>();
        SeriesRenderData seriesRenderDatapie = new SeriesRenderData();
        seriesRenderDatapie.setName("使用率area");
        seriesRenderDatapie.setValues(useRate.toArray(new Double[useRate.size()]));
//        seriesRenderDatapie.setComboType(SeriesRenderData.ComboType.AREA);
        seriesRenderDatasPie.add(seriesRenderDatapie);
        pie2.setSeriesDatas(seriesRenderDatasPie);
        data.put("myChart4", pie2);



        template.render(data);

        FileOutputStream out = new FileOutputStream("F:\\apps-other\\db\\template.docx");
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }
}
